﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.GuaZhiDuanLianService;
using CommunitySystem.Views;

namespace CommunitySystem
{
    public partial class GuaZhiEmpZhiWeiAnPaiControl : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private int ExameID;
        private string OrgIDs;
        private GuaZhiDuanLianServiceClient Client1 =
            new GuaZhiDuanLianServiceClient();
        public ObservableCollection<GuaZhiEmpModel> selectedempmodel =
            new ObservableCollection<GuaZhiEmpModel>();
        public GuaZhiEmpZhiWeiAnPaiControl(int exameid,string orgids,string status)
        {
            InitializeComponent();
            Client1.GetGuaZhiDuanLianEmpZongModelCompleted +=
               new EventHandler<GetGuaZhiDuanLianEmpZongModelCompletedEventArgs>(Client1_GetGuaZhiDuanLianEmpZongModelCompleted);
            ExameID = exameid;
            OrgIDs = orgids;
            Client1.GetGuaZhiDuanLianEmpZongModelAsync(ExameID, orgids, status, null);
        }

        void Client1_GetGuaZhiDuanLianEmpZongModelCompleted(object sender, GetGuaZhiDuanLianEmpZongModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
        }
        private Stream ss = null;
        private void ExportAll_Click(object sender, RoutedEventArgs e)
        {
            if(selectedempmodel.Count <= 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要导出的数据！");
                return;
            }

            ss = null;
            var downLoad = new SaveFileDialog()
            {
                Filter = "Excel files(2003) (*.xls)|*.xls",
            };
            if (downLoad.ShowDialog() == true)
            {
                ss = downLoad.OpenFile();
                PageInit();

            }
        }

        public void PageInit()
        {

        }


        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (GuaZhiEmpModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }
        }
    }
}
